package com.shuziren.controller.system;



import com.shuziren.core.vo.Result;
import com.shuziren.entity.SysMenu;
import com.shuziren.entity.vo.MenuVO;
import com.shuziren.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author CX330
 * @since 2025-05-04
 */
@RestController
@RequestMapping("/api/menu")
@CrossOrigin
public class SysMenuController {

    @Autowired
    private ISysMenuService menuService;

    /**
     * 查单页面全查
     * @return
     */
    @GetMapping("/list")
    public Result<List<MenuVO>> list() {
        List<MenuVO> menuTree = menuService.getMenuTree();
        return Result.success(menuTree);
    }

    /**
     * 查单页面添加包括子菜单添加
     * @param menu
     * @return
     */
    @PostMapping
    public Result<Long> add(@RequestBody SysMenu menu) {
        menuService.save(menu);
        return Result.success(menu.getId());
    }

    /**
     * 修改菜单名称以及路由
     * @param id
     * @param menu
     * @return
     */
    @PostMapping("/{id}")
    public Result<String> update(@PathVariable Long id, @RequestBody SysMenu menu) {
        menu.setId(id);
        menuService.updateById(menu);
        return Result.success("更新成功");
    }
    /**
     * 删除菜单
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        menuService.removeById(id);
        return Result.success("删除成功");
    }
}
